package com.android.svmc.app.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.svmc.app.mobile.activity.R;
import com.android.svmc.app.mobile.data.DownloadIMG;
import com.android.svmc.app.mobile.model.PictureModel;
import com.android.svmc.app.mobile.view.FontTextView;

public class ListPictureAdapter extends BaseAdapter {

	Context mcontext;
	private LayoutInflater mInflater;
	ArrayList<PictureModel> listpictures = new ArrayList<PictureModel>();
	private boolean online;
	
	public ListPictureAdapter(Context context,
			ArrayList<PictureModel> pictures) {
		mcontext = context;
		listpictures = pictures;
		mInflater = LayoutInflater.from(context);

	}
	public ListPictureAdapter(Context context,
			ArrayList<PictureModel> pictures,boolean online) {
		mcontext = context;
		listpictures = pictures;
		mInflater = LayoutInflater.from(context);
		this.online = online;

	}
	@Override
	public int getCount() {
		return listpictures.size();
	}

	@Override
	public Object getItem(int position) {
		return listpictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		PictureModel picture = listpictures.get(position);
		PictureHolder holder ;
		if(view ==null){
			mInflater = (LayoutInflater) mcontext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = mInflater.inflate(R.layout.item_list_all_picture, null);
			holder = new PictureHolder();
			
			holder.thumbnail =(ImageView)view.findViewById(R.id.item_picture_img_thumbnail);
			holder.title =(TextView)view.findViewById(R.id.item_picture_title);
			holder.date =(TextView)view.findViewById(R.id.item_picture_date);
			holder.totalComment =(TextView)view.findViewById(R.id.item_picture_count_comment);			
			view.setTag(holder);			
		}else {
			holder =(PictureHolder)view.getTag();
		}
		holder.title.setText(picture.getTitle());
		holder.date.setText(picture.getDate());
		holder.totalComment.setText(picture.getTotalComment());
		FontTextView.setfontMyriadPro_semiBold(holder.title, mcontext);
		
		String imgUrl = picture.getThumbnail();
		if( imgUrl != null){
			if (online ==true) {
				DownloadIMG download = new DownloadIMG(imgUrl);
				download.Start(holder.thumbnail, this);	
			}else {
				holder.thumbnail.setImageResource(R.drawable.icon_default);
			}
			
		}else {
			holder.thumbnail.setImageResource(R.drawable.icon_default);
		}
		return view;
	}
	static class PictureHolder{
		ImageView thumbnail;
		TextView title;
		TextView date;
		TextView totalComment;
	}

}
